/**
 * 
 */
package com.et114.modules.content.action;

import java.util.List;
import java.util.StringTokenizer;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.et114.components.simpletable.DataList;
import com.et114.core.action.BaseDispatchAction;
import com.et114.core.utility.FileUploadUtil;
import com.et114.core.utility.NumberUtils;
import com.et114.core.utility.StringPool;
import com.et114.core.utility.ThreadSynchronizer;
import com.et114.modules.content.services.SliderServiceImpl;
import com.et114.modules.content.vo.Slider;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.commons.Context;

/*
 * @author guanhw
 */
public class SliderAction extends BaseDispatchAction {
	public  ActionForward getSlider( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		SliderForm sliderForm = ( SliderForm ) form ; 
		Slider slider = new Slider( ) ; 
		PropertyUtils.copyProperties ( slider , sliderForm ) ; 

		Long id = slider.getSliderId ( ) ; 
		SliderServiceImpl sss = ( SliderServiceImpl ) getBean( Context.COMMON_SLIDER_SERVICE ) ;
		if ( NumberUtils.isEmptyOrZero ( id ) ) {  //loads
			request.setAttribute ( "searchvo" , slider ) ;
			DataList dl = sss.getSliders ( slider ) ;
			request.setAttribute ( "dl" , dl ) ; 
			return mapping.findForward ( "sliders" ) ;
		}
		else { //load one
			slider = sss.getSlider ( id ) ; 
			request.setAttribute ( "vo" , slider ) ; 
			return mapping.findForward ( "slider" ) ; 
		}
	}
	
	public ActionForward toEditSlider( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		SliderForm sliderForm = ( SliderForm ) form ; 
		Slider slider = new Slider( ) ; 
		PropertyUtils.copyProperties ( slider , sliderForm ) ; 
		request.setAttribute ( "vo" , slider ) ; 
		
		return mapping.findForward ( "slider" ) ; 
	}
	
	public ActionForward editSlider( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		SliderForm sliderForm = ( SliderForm ) form ; 
		Slider slider = new Slider( ) ; 
		PropertyUtils.copyProperties ( slider , sliderForm ) ; 
		
		slider.setUpdtUser ( getCurrentUser ( request ).getUserId ( ) ) ;
		Long id = slider.getSliderId ( ) ; 
		SliderServiceImpl sss = ( SliderServiceImpl ) getBean( Context.COMMON_SLIDER_SERVICE ) ;
		/*
		 * if ( NumberUtils.isEmptyOrZero ( id ) ) { //new id = sss.saveSlider (
		 * slider ) ; } else { //update sss.updateSlider ( slider ) ; }
		 */
		id = saveAndUpdateSlider( slider , sliderForm.getImagePathff ( ) , sss , request ) ; 
		return new ActionForward( "slider.go?method=getSlider&sliderId=" + id , true ) ; 
	}
	
	private Long saveAndUpdateSlider( Slider sliderVo , FormFile formFile  , SliderServiceImpl sss , HttpServletRequest request ) throws Exception {
		Long sliderId = sliderVo.getSliderId ( );
		String uploadFn = formFile.getFileName ( );
		if ( ! StringUtils.isEmpty ( uploadFn )
				&& ! StringUtils.isBlank ( uploadFn ) ) {
			FileUploadUtil upload = new FileUploadUtil ( );
			ThreadSynchronizer.threadEnter ( );
			try {
				upload.setRestrictfs ( 102400 * 5 );
				String fn = upload.uploadv2 ( formFile , request ,
						Const.PRO_SLIDER_IMG_UPLOAD_PATH , UUID.randomUUID ( )
								.toString ( ) );
				fn = Const.PRO_SLIDER_IMG_UPLOAD_PATH + fn;
				sliderVo.setImagePath ( fn );
			} finally {
				ThreadSynchronizer.threadExit ( );
			}
		}
		
		if ( !NumberUtils.isEmptyOrZero ( sliderId )  ) { // update
			Slider slider = sss.getSlider ( sliderId ) ; 
			String imagePath = slider.getImagePath ( ) ; 
			if ( StringUtils.isEmpty ( uploadFn ) || StringUtils.isBlank ( uploadFn ) ) 
				sliderVo.setImagePath ( imagePath ); 
			else {
				FileUploadUtil.removeRealPathFile ( imagePath , request ) ;
			}
			sss.updateSlider ( sliderVo ) ;	
		}
		else { //save
			sliderId = sss.saveSlider ( sliderVo ) ; 
		}
		
		//upload

		return sliderId ; 
	}	
	
	
	public ActionForward removeSlider( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		SliderForm sliderForm = ( SliderForm ) form ; 

		Long[] deleteIds =sliderForm.getDeleteId ( ) ; 
		
		//BIS
		SliderServiceImpl sss = ( SliderServiceImpl ) getBean( Context.COMMON_SLIDER_SERVICE ) ;
		
		if ( deleteIds != null ) 
			for ( Long deleteId : deleteIds ) 
				sss.removeSlider ( deleteId ) ;
	
		return new ActionForward( "slider.go?method=getSlider" , true ) ; 
	}
	
	public ActionForward toDisSlider( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		SliderForm sliderForm = ( SliderForm ) form ; 
		Slider slider = new Slider( ) ; 
		PropertyUtils.copyProperties ( slider , sliderForm ) ; 
		request.setAttribute ( "vo" , slider ) ; 
		
		SliderServiceImpl sss = ( SliderServiceImpl ) getBean( Context.COMMON_SLIDER_SERVICE ) ;
		List< Slider > ss =  sss.getSlidersNPage ( slider ) ; 
		request.setAttribute ( "ss" , ss ) ;
		return mapping.findForward ( "disOrder" ) ; 
	}
	
	
	public ActionForward editDisSlider( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		SliderForm sliderForm = ( SliderForm ) form ; 
		String orders = sliderForm.getOrders ( ) ; 
		
		Slider slider = new Slider( ) ; 
		PropertyUtils.copyProperties ( slider , sliderForm ) ; 
		request.setAttribute ( "vo" , slider ) ; 
		
		SliderServiceImpl sss = ( SliderServiceImpl ) getBean( Context.COMMON_SLIDER_SERVICE ) ;
		if ( !StringUtils.isEmpty ( orders ) && !StringUtils.isBlank ( orders ) ) {
			StringTokenizer st = new StringTokenizer( orders , StringPool.SEMICOLON ) ;
			int count = st.countTokens ( ) ; 
			Slider vo = new Slider( ) ; 
			for (  int i = 0 ; i <  count ; i ++ ) {
				Long orderId = Long.valueOf ( st.nextToken() );
				vo = sss.getSlider (  orderId ) ; 
				vo.setDisOrder (   Long.valueOf ( i + 1 ) ) ; 
				sss.updateSlider (  vo ) ;
			}
		}
		return new ActionForward( "slider.go?method=toDisSlider" , true ) ; 
	}
		
}
